package gpl.scotlandyard.ui.dialogs;

import gpl.scotlandyard.ui.tools.JDialogs;
import gpl.scotlandyard.ui.tools.ToolsFactory;
import gpl.scotlandyard.utils.I18n;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

/**
 * Starting dialog to get player names.
 * <p>Controls that names and password are not empty and a minimum of one detective is set.</p>
 * @author Norbert Martin
 */
public class LocalStartDialog {

  private final JDialog jdialog = new JDialog();
  private final DefaultListModel listModel = new DefaultListModel();
  private final JList listView = new JList(listModel);
  private final JTextField fugitiveName = new JTextField();
  private final JPasswordField fugitivePwd = new JPasswordField();
  private boolean isInit;
  private boolean isCancelled = true;

  /**
   * Sets visible.
   * @param b
   */
  public void setVisible(boolean b) {
    if (b && !isInit) {
      init();
    }
    jdialog.setVisible(b);
  }

  /**
   * Initializes content.
   */
  private void init() {
    JPopupMenu popupMenu = ToolsFactory.createAMDJPopupMenuOnList(listModel, listView);
    listView.setComponentPopupMenu(popupMenu);
    jdialog.setPreferredSize(new Dimension(400, 200));
    jdialog.setTitle(I18n.get("START_TITLE"));
    jdialog.setResizable(false);
    jdialog.setModal(true);

    listView.setVisibleRowCount(3);
    listView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    JScrollPane scroll = new JScrollPane(listView);

    JLabel fName = new JLabel(I18n.get("F_NAME"));
    JLabel fPwd = new JLabel(I18n.get("F_PWD"));
    JLabel dNames = new JLabel(I18n.get("D_NAMES"));
    final JButton ok = new JButton(I18n.get("START"));
    ok.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (fugitiveName.getText().isEmpty() || new String(fugitivePwd.getPassword()).isEmpty() || listModel.getSize() < 1) {
          JDialogs.showError(null, "Context incomplet");
          return;
        }
        isCancelled = false;
        setVisible(false);
      }
    });
    jdialog.setLayout(new BorderLayout());

    JPanel p = new JPanel(null);
    GroupLayout layout = new GroupLayout(p);
    p.setLayout(layout);

    layout.setAutoCreateContainerGaps(true);
    layout.setAutoCreateGaps(true);

    GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
    hGroup.addGroup(layout.createParallelGroup().addComponent(fName).addComponent(fPwd).addComponent(dNames));
    hGroup
        .addGroup(layout.createParallelGroup().addComponent(fugitiveName).addComponent(fugitivePwd).addComponent(scroll).addComponent(ok));
    layout.setHorizontalGroup(hGroup);

    GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
    vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(fName).addComponent(fugitiveName));
    vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(fPwd).addComponent(fugitivePwd));
    vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(dNames).addComponent(scroll));
    vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(ok));
    layout.setVerticalGroup(vGroup);

    jdialog.setContentPane(p);
    jdialog.pack();
    jdialog.setLocationRelativeTo(null);
    isInit = true;
  }

  /**
   * Returns the entered fugitive name.
   * @return fugitive name
   */
  public String getFugitiveName() {
    return fugitiveName.getText();
  }

  /**
   * Returns the entered fugitive password.
   * @return fugitive password
   */
  public String getFugitivePwd() {
    return new String(fugitivePwd.getPassword());
  }

  /**
   * Returns the list of detectives names.
   * @return detective names
   */
  public List<String> getDetectiveNames() {
    List<String> dnames = new ArrayList<String>(listModel.size());
    for (int i = 0; i < listModel.size(); i++) {
      dnames.add((String) listModel.get(i));
    }
    return dnames;
  }

  /**
   * Return true if was cancelled.
   * @return true if dialog close without click on start button.
   */
  public boolean isCancelled() {
    return isCancelled;
  }
}
